<div id="front-blurp">

<h1>Duktape</h1>

<p>Duktape is an <b>embeddable Javascript</b> engine,
with a focus on <b>portability</b> and compact <b>footprint</b>.</p>

<p>Duktape is easy to integrate into a C/C++ project: add <code>duktape.c</code>
and <code>duktape.h</code> to your build, and use the Duktape API to call Ecmascript
functions from C code and vice versa.</p>

<h1>Main features</h1>
<ul>
<li>Embeddable, portable, compact:
    <ul>
    <li>200kB code</li>
    <li>46kB startup RAM (x86, default options)</li>
    <li>22kB startup RAM (x86, <a href="https://github.com/svaarala/duktape/blob/master/doc/low-memory.rst">lowmem options</a>)</li>
    <li>45k<a href="http://cloc.sourceforge.net/">LoC</a> source (excluding comments etc)</li>
    <li>Can run on platforms with 256kB flash and 96kB system RAM</li>
    </ul>
</li>
<li><a href="http://www.ecma-international.org/ecma-262/5.1/">Ecmascript E5/E5.1</a>
    compliant, some features borrowed from
    <a href="https://people.mozilla.org/~jorendorff/es6-draft.html">E6 draft</a></li>
<li>Built-in debugger</li>
<li>Built-in regular expression engine</li>
<li>Built-in Unicode support</li>
<li>Minimal platform dependencies</li>
<li>Combined reference counting and mark-and-sweep garbage collection
    with finalization</li>
<li>Custom features like coroutines, built-in logging framework, and
    built-in CommonJS-based module loading framework</li>
<li>Property virtualization using a subset of Ecmascript E6 Proxy object</li>
<li>Liberal license (MIT)</li>
</ul>

<h1>Current status</h1>
<ul>
<li>Stable</li>
</ul>

<h1>Support</h1>
<ul>
<li>User community Q&amp;A: Stack Overflow <a href="http://stackoverflow.com/questions/tagged/duktape">duktape</a> tag</li>
<li>Bugs and feature requests: <a href="https://github.com/svaarala/duktape/issues">GitHub issues</a></li>
<li>General discussion: IRC <code>#duktape</code> on <code>chat.freenode.net</code> (<a href="https://webchat.freenode.net/">webchat</a>)</li>
</ul>

<h1>Some projects using Duktape (alphabetical)</h1>
<ul>
<li><a href="https://wiki.allseenalliance.org/_media/training/programming_alljoyn.js.pdf" target="_blank">AllJoyn.js</a>, using Duktape for embedded applications, <a href="https://git.allseenalliance.org/cgit/core/alljoyn-js.git/" target="_blank">git repo</a></li>
<li><a href="http://atomicgameengine.com/" target="_blank">Atomic Game Engine</a>, using Duktape for scripting</li>
<li><a href="http://store.steampowered.com/app/241240/" target="_blank">Contraption Maker</a>, using Duktape for
<a href="http://steamcommunity.com/workshop/browse/?appid=241240&amp;requiredtags[]=Mod" target="_blank">mods</a></li>
<li><a href="http://dukluv.io/" target="_blank">DukLuv.io</a>, libuv bindings for Duktape</li>
<li><a href="https://github.com/ndob/duktape-node" target="_blank">duktape-node</a>, running scripts in a separate isolated context from Node.js</li>
<li><a href="https://github.com/judofyr/duktape.rb" target="_blank">duktape.rb</a>, Ruby binding for Duktape</li>
<li><a href="https://github.com/olebedev/go-duktape" target="_blank">go-duktape</a>, Go binding for Duktape</li>
<li><a href="https://github.com/ldx/libpac" target="_blank">libpac</a>, using Duktape to parse proxy PAC files</li>
<li><a href="https://github.com/megous/megatools" target="_blank">Megatools</a>, using Duktape for main program logic</li>
<li><a href="http://www.radare.org/" target="_blank">radare</a>, using Duktape for reverse engineering scripts</li>
<li><a href="http://rarewire.com/" target="_blank">RareWire</a> application studio</li>
<li><a href="https://showtimemediacenter.com/" target="_blank">Showtime</a> media center, using Duktape for plugins</li>
</ul>

</div>  <!-- front-blurp -->

<div id="front-steps">

<h1><span class="step">1</span> Add to build</h1>

(See <a href="guide.html#gettingstarted">Getting started</a> for a more
detailed introduction.)

<p>Add Duktape C source and header to your build.  Any build system can
be used.  The distributable contains an example Makefile for reference.
In the simplest case:</p>

<pre>
$ gcc -std=c99 -o test test.c duktape.c -lm
$ ./test
Hello world!
</pre>

<h1><span class="step">2</span> Initialize a context</h1>

<p>Initialize and use Duktape somewhere in your program:</p>

<pre class="c-code">
/* test.c */
#include &quot;duktape.h&quot;

int main(int argc, char *argv[]) {
  duk_context *ctx = duk_create_heap_default();
  duk_eval_string(ctx, "print('Hello world!');");
  duk_destroy_heap(ctx);
  return 0;
}
</pre>

<h1><span class="step">3</span> Add C function bindings</h1>

<p>To call a C function from Ecmascript code, first declare your
C function:</p>

<pre class="c-code">
int adder(duk_context *ctx) {
  int i;
  int n = duk_get_top(ctx);  /* #args */
  double res = 0.0;

  for (i = 0; i &lt; n; i++) {
    res += duk_to_number(ctx, i);
  }

  duk_push_number(ctx, res);
  return 1;  /* one return value */
}
</pre>

<p>Register your function e.g. into the global object:</p>

<pre class="c-code">
duk_push_global_object(ctx);
duk_push_c_function(ctx, adder, DUK_VARARGS);
duk_put_prop_string(ctx, -2 /*idx:global*/, "adder");
duk_pop(ctx);  /* pop global */
</pre>

<p>You can then call your function from Ecmascript code:</p>

<pre class="c-code">
duk_eval_string(ctx, "print('2+3=' + adder(2, 3));");
duk_pop(ctx);  /* pop eval result */
</pre>

</div>  <!-- front-steps -->

